package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.EnterpriseTypeDAO;
import cn.edu.nju.software.greencreditsystem.domain.EnterpriseType;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-2
 * 类描述：EnterpriseType类DAO实现
 */
public class EnterpriseTypeDAOImpl implements EnterpriseTypeDAO
{

    @Override
    public boolean save(EnterpriseType et) 
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        try
        {
            s.beginTransaction();
            s.save(et);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(EnterpriseType et) 
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        try
        {
            s.beginTransaction();
            s.delete(et);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public EnterpriseType getById(int id) 
    {
        EnterpriseType result = null;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            result = (EnterpriseType) s.get(EnterpriseType.class, id);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public EnterpriseType getByType(String type) 
    {
        EnterpriseType result = null;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from EnterpriseType et where et.type='" + type + "'");
            result = (EnterpriseType) q.uniqueResult();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(EnterpriseType et) 
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        try
        {
            s.beginTransaction();
            s.update(et);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<EnterpriseType> getAll() 
    {
        List<EnterpriseType> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            Query q = s.createQuery("from EnterpriseType");
            result = (List<EnterpriseType>) q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public int count() 
    {
        int result = -1;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            Query q = s.createQuery("from EnterpriseType");
            result = q.list().size();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }
    
}
